﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LxCFD
{
    /// <summary>
    /// ColorBar.xaml 的交互逻辑
    /// </summary>
    public partial class ColorBar : UserControl
    {
        public ColorBar()
        {
            InitializeComponent();
        }
        public void LoadColor(ColorRange colorRange)
        {
            this.ColorCanvas.Children.Clear();
            if (colorRange == null)
            {
                this.Visibility = Visibility.Collapsed;
                //Background = Brushes.Transparent;
            }
            else
            {
                this.Visibility = Visibility.Visible;

                this.Background = new LinearGradientBrush()
                {
                    StartPoint = new Point(0, 1),
                    EndPoint = new Point(0, 0),
                    GradientStops = colorRange.ColorBarBrush.GradientStops,
                };
                double inter = (colorRange.Range.Max - colorRange.Range.Min) / colorRange.Segment;
                double height = this.Height / colorRange.Segment;
                for (int i = 0; i <= colorRange.Segment; i++)
                {
                    TextBlock te = new TextBlock()
                    {
                        Height = 20,
                        Text = (colorRange.Range.Min + i * inter).ToString("e3")
                    };
                    this.ColorCanvas.Children.Add(te);
                    Canvas.SetLeft(te, this.Width + 5);
                    Canvas.SetBottom(te, height * i - te.Height / 2);
                }
            }
        }
        //public GradientStopCollection GetSteppedGradientBrush(Color[] colors)
        //{
        //    var brush = new GradientStopCollection();
        //    double n = colors.Length;
        //    for (int i = 0; i < colors.Length; i++)
        //    {
        //        var gs0 = new GradientStop(colors[i], i / n);
        //        var gs1 = new GradientStop(colors[i], (i + 1) / n);
        //        brush.Add(gs0);
        //        brush.Add(gs1);
        //    }
        //    return brush;
        //}
        //public Color[] GetRainbowColors(int seg)
        //{
        //    Color[] c = new Color[seg];
        //    float segd = seg - 1;
        //    for (int i1 = 0; i1 < seg; i1++)
        //    {
        //        float pos = i1 / segd;
        //        c[i1] = Color.FromRgb(r(pos), g(pos), b(pos));
        //    }
        //    return c;
        //    byte r(float p)
        //    {
        //        if (p < 0.5) return 0;
        //        if (p < 0.75) return (byte)((p - 0.5) * (255 * 4));
        //        return 255;
        //    }
        //    byte g(float p)
        //    {
        //        if (p < 0.25) return (byte)(p * (255 * 4));
        //        if (p < 0.75) return 255;
        //        return (byte)(255 - (p - 0.75) * (255 * 4));
        //    }
        //    byte b(float p)
        //    {
        //        if (p < 0.25) return 255;
        //        if (p < 0.5) return (byte)(255 - (p - 0.25) * (255 * 4));
        //        return 0;
        //    }
        //    //}
        //}
    }
}
